A method to find the first 3n+5 odd integers with alternating bit sum -n
------------------------------------------------------------------------

    There are five simple rules to find the first 3n+5 odd integers i, in
ascending order, having alternating bit sum S(i) equal to a given -n. The first
rule is,

I - The least integer L such that S(L) = -n is L = (2/3)*(4^n-1) , Cf. A020988.

    The value of L is always even because the last digit of L is 0 or 2. So L is
composed, except for n=1. for n>=1 the number of bits of L is 2n since the
binary representation of L has n bits one, and also n bits zero.

    In what follows "odd bits" mean the bits of a number corresponding to the
odd powers of two. The other bits will be the "even bits". In a number i such
that S(i) = -n, there are an excess of n odd bits, i.e.,

n = (number of odd bits of i) - (number of even bits of i).

    Since L is even, we turn-on the LSB of L to make an odd number, say x.
Because S(x) = S(L)+1, we have to add an odd bit b to x to compensate. The bit b
goes to the left of the MSB of x because all the odd bits of x are ones. So the
first odd number i_1 such that S(i_1) = -n, is given by the rule,

II - i_1 = L + 1 + 2^(2n+1) = (2/3)(4^n-1) + 1 + 2^(2n+1).

    For n>=2, hence L has 2n bits, the MSB of L corresponds to 2^(2n-1), and the
odd bit added corresponds to 2^(2n+1). For example, if n = 4,

L = 10101010, so i_1 = 10 || 10101011 = 1010101011.

The rule II can be implemented by the PARI function,

II()={
i = (2/3)*(4^n-1) + 1 + 2^(2*n+1);
if(isprime(i),
 an++ )
};

    The variables i, n, and an are global. The if statement was added to II()
since in A184908 we count the primes in the first 3n+5 odd values of i.

    Because all the odd bits of i_1 are ones, we cannot turn-off any odd bit,
and to find i_2, we have to add an odd bit. (If we turn-on an even bit, we also
have to add an odd bit, and the number obtained would be greater than i_2.) From
that considerations We find the next n+1 values i_2, ... , i_(n+2) by the rule,

III - To find i_2 we begin by adding an odd bit to i_1. This bit corresponds to
2^(2n+3) because of the bit added before. i_2 is determined turning-off the bit
2^(2n+1). (We begin turning-off the bit 2n+1 to give values of i in ascending
order.) We continue turning-on the last bit turned-off and turning-off the next
odd bit to the right. There are n+1 iterations because we begin with 2^(2n+3)
and end at 3.

The rule III, that determines i_2 to i_(n+2) can be implemented by the PARI function,

III()={
w = 2^(2*n+3);
for(j=1, n+1,
 i += w;
 w /= 4;
 i -= w;
 if(isprime(i),
  an++ )
 )
};

For example, for n=4, we have i_1, and the next n+1 values of i depicted below

              1
Powers of two 109876543210
        i_1 =   1010101011
                v
        i_2 = 100010101011
                v v
              101000101011
                  v v
              101010001011
                    v v
              101010100011
                      v v
        I_6 = 101010101001


    Now, the unique odd bit zero is the bit 1, so we set it, compensing with the
even bit to the left. So we obtain the next n+1 values i_(n+3), ... , i_(2n+3)
by the rule,

IV - turn-on both the bits 1, and b = 2, this gives i_(n+3). Turn-off b, and
turn-on the next bit to the left of b. Repeat until b is the bit 2n+2. Since b
varies from 2 to 2n+2, we have n+1 numbers i_(n+3) to i_(2n+3).

The rule IV can be implemented by the PARI function,

IV()={
i+=6;
if(isprime(i),
 an++ );
w=4;
for(j=1, n,
 i -= w;
 w *= 4;
 i += w;
 if(isprime(i),
  an++ )
 )
};

For example, when n = 4, we have i_6, plus i_7 to i_11 depicted below.

                11
Powers of two   109876543210
        i_6 =   101010101001
                         vv
                101010101111
                       v v
                101010111011
                     v v
                101011101011
                   v v
                101110101011
                 v v
        i_11 =  111010101011


    Because the MSB is an odd bit, to find i_(2n+4) we add an even bit to the
left of the MSB, and turn-off the unique even bit b, different from 0, which is
one, the bit b = 2n+2. For example, when n = 4, we have,

i_11 =  111010101011
         v
i_12 = 1101010101011

    From i_(2n+4) we cannot go to any place, since all the bits cannot be
modified. So we add an odd bit to the left of i_(2n+4), making a number x such
that S(x) = -(n+1). For example, when n = 4, we have,

x = 1 || 1101010101011 = 11101010101011.

    Now we turn-off the even bit b = 2n+4 of x, to found w  such that S(w) =
-(n+2). For example, when n = 4, we have,

x = 11101010101011
     v
w = 10101010101011

    To compensate we turn-off the two most significant odd bits of w, that are
the bits 2n+3, and 2n+1, and found i_(2n+5). For example, when n = 4, we have,

              w = 10101010101011
                    v v
i_(2n+5) = i_13 = 10000010101011

    Now that the numbers of odd and even bits are balanced, we proceed with the
least number greater than i_(2n+5), that is found turning on the least
significative odd bit b equal to zero, and turning off the next odd bit to the
right. Since b is 2n+1, we determine n values ending with b = 3. For example,
when n = 4, we have,

i_13 = 10000010101011
           v v
       10001000101011
             v v
       10001010001011
               v v
       10001010100011
                 v v
i_17   10001010101001


    With the above considerations, the rule V can be implemented by the PARI function,

V()={
i += 2^(2*n+4) - 2^(2*n+2);
if(isprime(i),
 an++ );
w = i + 2^(2*n+5) - 2^(2*n+4);
i = w - 2^(2*n+3) - 2^(2*n+1);
if(isprime(i),
 an++ );
w = 2^(2*n+1);
for(j=1, n,
 i += w;
 w /= 4;
 i -= w;
 if(isprime(i),
  an++ )
 )
};
-------------------------
